﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://www.codeplex.com/EmlenMud
using System.Runtime.Serialization;
using System;
using System.Threading;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
using System.Text;
using BrainTechLLC.ThreadSafeObjects;
using System.ComponentModel;
using System.IO;
using Mud.EmlenMudMain;
using System.Diagnostics;

namespace BrainTechLLC
{
    [Serializable]
    [DataContract]
    public class Area : GameThingBaseClass
    {
        [DataMember]
        public AssociatedIDCollection _rooms;

        public AssociatedIDCollection Rooms
        {
            get
            {
                if (_rooms == null)
                    Interlocked.CompareExchange<AssociatedIDCollection>(ref _rooms, new AssociatedIDCollection(), null);

                return _rooms;
            }
        }

        [DataMember]
        public string _originalFileName = "";

        public string OriginalFileName
        {
            get { return _originalFileName; }
            set
            {
                if (!_originalFileName.AreEqual(value))
                {
                    OnPropertyChanging("OriginalFileName", _originalFileName, value);
                    _originalFileName = value;
                    OnPropertyChanged("OriginalFileName", _originalFileName, value);
                };
            }
        }

        [DataMember]
        public string _fileName = "";

        public string FileName
        {
            get { return _fileName; }
            set
            {
                if (!_fileName.AreEqual(value))
                {
                    OnPropertyChanging("FileName", _fileName, value);
                    _fileName = value;
                    OnPropertyChanged("FileName", _fileName, value);
                };
            }
        }

        [DataMember]
        public AreaFlags _areaFlags;

        public AreaFlags AreaFlags /* Area flags, changed, etc */
        {
            get { return _areaFlags; }
            set
            {
                if (!_areaFlags.AreEqual(value))
                {
                    OnPropertyChanging("AreaFlags", _areaFlags, value);
                    _areaFlags = value;
                    OnPropertyChanged("AreaFlags", _areaFlags, value);
                };
            }
        }
        
        [DataMember]
        public List<string> _builders;

        public string BuilderList
        {
            get
            {
                if (_builders == null || _builders.Count == 0)
                    return "(None)";

                return _builders.BuildList((s) => s);
            }
        }

        public List<string> Builders
        {
            get
            {
                if (_builders == null)
                    Interlocked.CompareExchange<List<string>>(ref _builders, new List<string>(), null);

                return _builders;
            }
            set
            {
                if (!_builders.AreEqual(value))
                {
                    OnPropertyChanging("Builders", _builders, value);
                    _builders = value;
                    OnPropertyChanged("Builders", _builders, value);
                };
            }
        }
        
        [DataMember]
        public int _vnumLow;

        public int VnumLow
        {
            get { return _vnumLow; }
            set
            {
                if (!_vnumLow.AreEqual(value))
                {
                    OnPropertyChanging("VnumLow", _vnumLow, value);
                    _vnumLow = value;
                    OnPropertyChanged("VnumLow", _vnumLow, value);
                };
            }
        }

        [DataMember]
        public int _vnumHigh;

        public int VnumHigh
        {
            get { return _vnumHigh; }
            set
            {
                if (!_vnumHigh.AreEqual(value))
                {
                    OnPropertyChanging("VnumHigh", _vnumHigh, value);
                    _vnumHigh = value;
                    OnPropertyChanged("VnumHigh", _vnumHigh, value);
                };
            }
        }

        [NonSerialized]
        public static ThreadSafeLookup<string, Area> AreaLookup = new ThreadSafeLookup<string, Area>();

        public static Area FindArea(string areaName)
        {
            Area result;

            if (AreaLookup.TryGetValue(areaName, out result))
                return result;
            
            List<Area> allAreas = Everything.FindAllThingsOfType<Area>();
            
            foreach (var area in allAreas)
            {
                if (area.Keywords.IndexOf(areaName, StringComparison.OrdinalIgnoreCase) >= 0)
                {
                    AreaLookup.AddOrSet(areaName, area);
                    return area;
                }
            }

            // TODO: fuzzy searching
            AreaLookup.AddOrSet(areaName, null);
            return null;
        }
    }
 }
